package values

import (
	"regexp"
	"testing"
)

func Test_CheckEmail(t *testing.T) {
	cases := []struct {
		input    string
		expected bool
	}{
		{"test@example.com", true},
		{"123@example.com", true},
		{"a-test@example.cn", true},
		{"test.cn@example.com.cn", true},
		{"a_.test@example.com", false},
		{"@example.cc", false},
		{".@example.c", false},
		{".@example.", false},
		{"example.c@", false},
		{"test.example.com", false},
		{"", false},
		{"test@example", false},
		{"test@example.", false},
		{"test@.com", false},
		{"test@com", false},
	}

	for _, c := range cases {
		result := IsEmail(c.input)
		if result != c.expected {
			t.Errorf("CheckEmail(%s) == %t, expected %t", c.input, result, c.expected)
		}
	}
}

func Test_CheckMobile(t *testing.T) {
	cases := []struct {
		input    string
		expected bool
	}{
		{"13812345678", true},
		{"15911631932", true},
		{"17155652242", true},
		{"17255652242", true},
		{"13052545114", true},
		{"19952545114", true},
		{"29952545114", false},
		{"39952545114", false},
		{"49952545114", false},
		{"138-1234-5678", false},
		{"1234567890", false},
		{"", false},
		{"1381234567", false},
		{"138123456789", false},
		{"13x12345678", false},
		{"abc12345678", false},
	}

	for _, c := range cases {
		result := IsChineseMobile(c.input)
		if result != c.expected {
			t.Errorf("CheckMobile(%s) == %t, expected %t", c.input, result, c.expected)
		}
	}
}
func Benchmark_CheckEmail(b *testing.B) {
	cases := []struct {
		input    string
		expected bool
	}{
		{"test@example.com", true},
		{"123@example.com", true},
		{"a-test@example.cn", true},
		{"test.cn@example.com.cn", true},
		{"@example.cc", false},
		{".@example.c", false},
		{"test.example.com", false},
		{"", false},
		{"test@example", false},
		{"test@example.", false},
		{"test@.com", false},
		{"test@com", false},
	}
	for i := 0; i < b.N; i++ {
		for _, c := range cases {
			result := IsEmail(c.input)
			if result != c.expected {
				b.Errorf("CheckEmail(%s) == %t, expected %t", c.input, result, c.expected)
			}
		}
	}
}
func Benchmark_CheckMobile(b *testing.B) {
	cases := []struct {
		input    string
		expected bool
	}{
		{"13812345678", true},
		{"15911631932", true},
		{"17155652242", true},
		{"17255652242", true},
		{"13052545114", true},
		{"19952545114", true},
		{"138-1234-5678", false},
		{"1234567890", false},
		{"", false},
		{"1381234567", false},
		{"138123456789", false},
		{"13x12345678", false},
		{"abc12345678", false},
	}
	for i := 0; i < b.N; i++ {
		for _, c := range cases {
			result := IsChineseMobile(c.input)
			if result != c.expected {
				b.Errorf("CheckMobile(%s) == %t, expected %t", c.input, result, c.expected)
			}
		}
	}
}
func Benchmark_CheckMobile_withReg(b *testing.B) {
	cases := []struct {
		input    string
		expected bool
	}{
		{"13812345678", true},
		{"15911631932", true},
		{"17155652242", true},
		{"17255652242", true},
		{"13052545114", true},
		{"19952545114", true},
		{"138-1234-5678", false},
		{"1234567890", false},
		{"", false},
		{"1381234567", false},
		{"138123456789", false},
		{"13x12345678", false},
		{"abc12345678", false},
	}
	var mobileRegex = regexp.MustCompile(`^1[3-9]\d{9}$`)
	for i := 0; i < b.N; i++ {
		for _, c := range cases {
			result := mobileRegex.MatchString(c.input)
			if result != c.expected {
				b.Errorf("CheckMobile(%s) == %t, expected %t", c.input, result, c.expected)
			}
		}
	}
}
